package com.tb.controller;

import com.tb.dubbo.IKindService;
import com.tb.pojo.Kind;
import com.tb.pojo.User;
import com.tb.utils.Msg;
import com.tb.utils.ReturnList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class KindController {
    @Autowired
    private IKindService kindService;

    /**
     * @api {get} /Kind 查询种类接口  杜飞虎
     * @apiDescription 查询当前种类目录及其详情信息
     * @apiGroup Kind API
     *
     * @apiParam {int} kindId 种类id
     * @apiParamExample {json} 请求参数格式:
     * {
     *     "kindId":"xxxxxx"
     * }
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *  {
     * 	"code": 0,
     * 	"msg": "",
     * 	"data": {
     * 		"total": 1,
     * 		"list": [{
     * 			"id": null,
     * 			"goodsType": "服装",
     * 			"fId": 0
     * 		}]
     * 	}
     * }
     *
     *  @apiErrorExample {json} 错误返回值:
     *           {
     *           "code": 999,
     *           "msg": "登陆超时",
     *           "data": null
     *           }
     *
     * @apiSampleRequest /Kind
     *
     */
    @GetMapping("/Kind")
    public Msg getKinds(@RequestParam(name = "kindId",defaultValue = "0") int kindId,HttpSession httpSession){
        try {
            User user = (User) httpSession.getAttribute( "AdminUser" );
            user.getId();

            Map<String,Object> map = new HashMap<>();
            map.put( "kindId",kindId );
            List<Kind> list = kindService.selecetKinds(map);
            long num =list.size();
           return  Msg.success( ReturnList.get(num,list));
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }

    /**
     * @api {post} /Kind 增加种类接口  杜飞虎
     * @apiDescription 在当前种类目录下增加一个子种类
     * @apiGroup Kind API
     *
     * @apiParam {Integer}  id 种类id
     * @apiParam {String}  goodsType 种类名称
     * @apiParam {Integer}  fId 上一级种类id 如果不写则视为一级目录
     *
     * @apiParamExample {json} 请求参数格式:
     *      {
     *     "goodsType":"电子产品",
     *     "fId":0
     *       }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *   "code": 0,
     *   "msg": "",
     *   "data": "添加成功"
     *      }
     *@apiErrorExample {json} 错误返回值:
     *      {
     *   "code": 1,
     *   "msg": "种类不能为空",
     *   "data": null
     *      }
     *
     *@apiErrorExample {json} 错误返回值:
     *           {
     *           "code": 999,
     *           "msg": "登陆超时",
     *           "data": null
     *           }
     *
     * @apiSampleRequest /Kind
     */
    @PostMapping("/Kind")
    public Msg addKind(@RequestBody  Kind kind, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            User user =(User) session.getAttribute( "AdminUser" );
            user.getUserId();

            if(kind.getGoodsType()==null||"".equals( kind.getGoodsType() )){
               return Msg.error(1,"种类不能为空");
           }
           if(kind.getfId()==null){
               kind.setfId( 0 );
           }
           //post put请求不需要处理中文乱码 get请求才需要
          // kind.setGoodsType( Utf8.getString( kind.getGoodsType() ));
            int num =kindService.addKind(kind);
           if(num!=0){
               return Msg.success( "添加成功" );
           }else {
               return Msg.error( 1,"添加失败" );
           }
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }
    /**
     * @api {put} /Kind 修改种类接口  杜飞虎
     * @apiDescription 修改当前种类
     * @apiGroup Kind API
     *
     * @apiParam {Integer}  id 种类id
     * @apiParam {String}  goodsType 种类名称
     * @apiParam {Integer}  fId 上一级种类id 一级目录为0
     *
     * @apiParamExample {json} 请求参数格式:
     *   {
     *     "id":2,
     *     "goodsType":"电子产品",
     *     "fId":0
     *   }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *      {
     *   "code": 0,
     *   "msg": "",
     *   "data": "修改成功"
     *      }
     * @apiErrorExample {json} 错误返回值:
     *      {
     *   "code": 1,
     *   "msg": "种类不能为空",
     *   "data": null
     *      }
     *
     *@apiErrorExample {json} 错误返回值:
     *      {
     *           "code": 999,
     *           "msg": "登陆超时",
     *           "data": null
     *       }
     *
     * @apiSampleRequest /Kind
     */
    @PutMapping ("/Kind")
    public Msg updateKind(@RequestBody Kind kind, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            User user =(User) session.getAttribute( "AdminUser" );
            user.getUserId();
            if(kind.getId()==null){
                return Msg.error(1,"请选择");
            }
            if(kind.getGoodsType()==null||"".equals( kind.getGoodsType() )){
                return Msg.error(1,"种类不能为空");
            }
            //kind.setGoodsType( Utf8.getString( kind.getGoodsType() ));
            int num =kindService.updateKind(kind);
            if(num!=0){
                return Msg.success( "修改成功" );
            }else {
                return Msg.error( 1,"修改失败" );
            }
        }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }
    /**
     * @api {delete} /Kind 种类删除  杜飞虎
     * @apiDescription  删除当前种类及其所有子种类
     * @apiGroup Kind API
     *
     * @apiParam  {Int} kindId 种类id
     *
     * @apiParamExample {json}  请求参数格式:
     *
     * {
     *     "kindId": 10
     * }
     *
     * @apiVersion 1.0.0
     *
     * @apiSuccessExample {json} 正确返回值:
     *     {
     *        "code": 0,
     *         "msg": "",
     *         "data":"SUCCESS"
     *      }
     *
     * @apiErrorExample {json} 错误返回值:
     *       {
     *           "code": 1,
     *           "msg": "failed",
     *           "data": null
     *       }
     * @apiErrorExample {json} 错误返回值:
     *       {
     *            "code": 999,
     *            "msg": "登陆超时",
     *            "data": null
     *        }
     *
     * @apiSampleRequest /Kind
     */
    @DeleteMapping ("/Kind")
    public Msg deleteKind( @RequestParam(value = "kindId",defaultValue = "0") int kindId, HttpServletRequest request){
        try {
            HttpSession session = request.getSession();
            User user =(User) session.getAttribute( "AdminUser" );
            user.getUserId();

            if (kindId == 0) {
                return Msg.error(1, "failed");
            }
            int num =kindService.deleteKind(kindId);
            if(num!=0){
                return Msg.success( "SUCCESS" );
        }else {
            return Msg.error( 1,"failed" );
        }
    }catch (Exception e){
            return Msg.error( 999,"登录超时！" );
        }
    }
}
